﻿using ConfExpert.Forms.Base;
using DevTeam;
using System;
using System.Data;

namespace ConfExpert.Forms.Aksesuar
{
    public partial class YikamaTalimati : XForm
    {
        private string SPName { get; set; }
        public YikamaTalimati(string SpName)
        {
            InitializeComponent();
            this.SPName = SpName;
        }

        private void YikamaTalimati_Load(object sender, EventArgs e)
        {
            DataSet ds = ExecuteDataSet(SPName, cbTumu.Checked);
            gridView1.Init(ds.Tables[0]);
            gvYikama.Init(ds.Tables[1]);
            
            //gridView1.Init(SPName, cbTumu.Checked);
            //  gvYikama.Init(SPName, cbTumu.Checked);
            //gridView1.AddDetailGrid(gvYikama, "Musteri, DosyaNo, Model");

            gvYikama.MakeColumnInvisible("idx");

            if (ytk(205))
            {
                GridManager gm = GridManager.GetManager(gvYikama);
                gm.GridMenu.AddMenu("Basıldı", YikamaTalimatiBasildi);
            }

            gvYikama.SetFilterTypeToCheckedList("Musteri", "DosyaNo");

            gridView1.MakeReadOnly();
            gvYikama.MakeReadOnly();

            bbiGuncelle.ItemClick += bbiGuncelle_ItemClick;
            cbTumu.CheckedChanged += cbTumu_CheckedChanged;

            gridView1.AddSummary("Adet");
            gvYikama.AddSummary("Adet");

            gridView1.BestFitColumns();
            // gvYikama.BestFitColumns();

            string Musteriler = gridView1.GetSelectedValues("Musteri").Replace(",", "','");
            string DosyaNolar = gridView1.GetSelectedValues("DosyaNo").Replace(",", "','");
            //string Modeller = gridView1.GetSelectedValues("Model").Replace(",", "','");
            gvYikama.ActiveFilterString = "Musteri in ('" + Musteriler + "') AND DosyaNo in ('" + DosyaNolar + "')"; //" AND Model in ('" + Modeller + "')";
            gvYikama.BestFitColumns();

            btnExport.ItemClick += btnExport_ItemClick;
            gridView1.SelectionChanged += gridView1_SelectionChanged;
        }

        void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            string Musteriler = gridView1.GetSelectedValues("Musteri").Replace(",", "','");
            string DosyaNolar = gridView1.GetSelectedValues("DosyaNo").Replace(",", "','");
            //string Modeller = gridView1.GetSelectedValues("Model").Replace(",", "','");
            gvYikama.ActiveFilterString = "Musteri in ('" + Musteriler + "') AND DosyaNo in ('" + DosyaNolar + "')"; //" AND Model in ('" + Modeller + "')";
            gvYikama.BestFitColumns();
        }

        void btnExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            gvYikama.EndEditing();

            gvYikama.ExportToExcel();
        }

        void cbTumu_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bbiGuncelle.PerformClick();
        }

        void bbiGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //gvYikama.RefreshDataSource(SPName, cbTumu.Checked);
            DataSet ds = ExecuteDataSet(SPName, cbTumu.Checked);
            gridView1.Init(ds.Tables[0]);
            gvYikama.Init(ds.Tables[1]);

            gvYikama.MakeColumnInvisible("idx");
            gvYikama.SetFilterTypeToCheckedList("Musteri", "DosyaNo");
            gridView1.MakeReadOnly();
            gvYikama.MakeReadOnly();
            gridView1.AddSummary("Adet");
            gvYikama.AddSummary("Adet");
            gridView1.BestFitColumns();
            string Musteriler = gridView1.GetSelectedValues("Musteri").Replace(",", "','");
            string DosyaNolar = gridView1.GetSelectedValues("DosyaNo").Replace(",", "','");
            
            gvYikama.ActiveFilterString = "Musteri in ('" + Musteriler + "') AND DosyaNo in ('" + DosyaNolar + "')"; //" AND Model in ('" + Modeller + "')";
            gvYikama.BestFitColumns();
        }

        private void YikamaTalimatiBasildi(object sender, EventArgs e)
        {
            foreach (int rowhandle in gvYikama.GetSelectedRows())
            {
                if (rowhandle > -1)
                    gvYikama.SetRowCellValue(rowhandle, "BasildiMi", true);
            }
            gvYikama.EndEditing();

            DataRow[] drSecilen = gvYikama.DataTable().Select("Isnull(BasildiMi,0) = 1");

            if (drSecilen.Length > 0)
            {
                string xml = gvYikama.SerializeChanges();
                ExecuteNonQuery("UpdYikamaTalimatiBasildi", xml, WhoAmI);
            }
        }

    }
}
